package com.example.last.controller;


import cn.hutool.core.date.DateUtil;
import com.example.last.common.Result;
import com.example.last.dao.DoctorDao;
import com.example.last.entity.Doctor;
import com.example.last.entity.Reservation;
import com.example.last.entity.Params;
import com.example.last.entity.Type;
import com.example.last.service.ReservationService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RequestMapping("/reservation")
@RestController
public class ReservationController {
    @Resource
    private ReservationService reservationService;
    @Resource
    private DoctorDao doctorDao;

    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        PageInfo<Reservation> info = reservationService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping
    public Result save(@RequestBody Reservation reservation){
        //剩余量时
        Doctor doctor = doctorDao.selectByPrimaryKey(reservation.getDoctorId());
        //if(doctor.getNum() == 0) {return Result.error("-------------")}；

        //往预约表插入预约信息
        reservation.setTime(DateUtil.now());
        reservationService.add(reservation);
        //医师疗愈次数记录
        doctorDao.update(doctor);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        reservationService.delete(id);
        return Result.success();
    }

}
